package com.dark.kafka.trident;

import backtype.storm.tuple.Fields;
import backtype.storm.tuple.Values;
import org.json.simple.JSONValue;
import storm.trident.operation.BaseFunction;
import storm.trident.operation.TridentCollector;
import storm.trident.tuple.TridentTuple;

import java.util.Map;

/**
 * Created by tengxue on 16-7-13.
 */
public class JsonProjectFunction extends BaseFunction {

    private Fields fields;

    public JsonProjectFunction(Fields fields){
        this.fields=fields;
    }
    @Override
    public void execute(TridentTuple tuple, TridentCollector collector) {
        String json=tuple.getString(0);
        Map<String,Object> map=(Map<String,Object>)JSONValue.parse(json);
        Values values=new Values();
        for (int i=0;i<fields.size();i++){
            values.add(map.get(this.fields.get(i)));
        }
        System.out.println("the values is "+values.toString());
        collector.emit(values);
    }
}
